package com.chujie.chujiechat.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.chujie.chujiechat.AppCache;
import com.chujie.chujiechat.R;
import com.chujie.chujiechat.model.Callback;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText etAccount;
    private EditText etToken;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        etAccount = findViewById(R.id.et_account);
        etToken = findViewById(R.id.et_token);
    }

    @Override
    public void onClick(View v) {
       Thread thread =  new Thread(){
           @Override
            public void run() {
                AppCache.getService().login(etAccount.getText().toString(), etToken.getText().toString(), new Callback<Void>() {
                    @Override
                    public void onEvent(Context context,int code, String msg, Void aVoid) {
                        if (code == 200) {
                            Toast.makeText(context, "登录成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(context, MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                            finish();
                        } else {
                            Toast.makeText(context, "登录失败 code=" + code + ", msg=" + msg, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
       };
       thread.start();

    }
}
